package com.dlyk.service.impl;

import com.alibaba.excel.EasyExcel;
import com.dlyk.config.listener.UploadDataListener;
import com.dlyk.constant.Constants;
import com.dlyk.mapper.TClueMapper;
import com.dlyk.model.TActivity;
import com.dlyk.model.TClue;
import com.dlyk.model.TUser;
import com.dlyk.query.BaseQuery;
import com.dlyk.query.ClueQuery;
import com.dlyk.service.ClueService;
import com.dlyk.util.JWTUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: ajie
 * @date: 2024/7/24 20:48:59
 * @version: 1.0
 */
@Service
public class ClueServiceImpl implements ClueService {

    @Resource
    private TClueMapper tClueMapper;

    @Override
    public PageInfo<TClue> getClueByPage(Integer current,ClueQuery clueQuery) {
        //设置PageHelper的分页参数
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //查询
        List<TClue> list = tClueMapper.selectClueByPage(clueQuery);
        //封装分页参数到PageInfo
        PageInfo<TClue> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public void importExcel(MultipartFile file,String token) throws IOException {
        //链式编程：3个参数，第一个参数是要读取的Excel文件，第二个参数是Excel模板类型，第三个参数是读取Excel的监听器
        EasyExcel.read(file.getInputStream(), TClue.class, new UploadDataListener(tClueMapper,token))
                .sheet()
                .doRead();
    }

    @Override
    public boolean checkPhone(String phone) {
        int count = tClueMapper.selectByCount(phone);
        return count <= 0;//没有查到手机号就是true
    }

    @Override
    public int saveClue(ClueQuery clueQuery) {
        int count = tClueMapper.selectByCount(clueQuery.getPhone());
        if (count>0){
            throw new RuntimeException("该手机号码已经录入过，不能再录入");
        }

        TClue tClue = new TClue();

        BeanUtils.copyProperties(clueQuery,tClue);

        tClue.setCreateTime(new Date());//创建时间

        ///登陆人的id
        Integer loginUserId = JWTUtils.parseJWTByUserId(clueQuery.getToken());
        tClue.setCreateBy(loginUserId);//创建人

        return tClueMapper.insertSelective(tClue);
    }

    @Override
    public TClue getClueById(Integer id) {
        return tClueMapper.selectDetailById(id);
    }

    @Override
    public int updateClue(ClueQuery clueQuery) {
        TClue tClue = new TClue();

        BeanUtils.copyProperties(clueQuery,tClue);

        tClue.setEditTime(new Date());//编辑时间

        ///登陆人的id
        Integer loginUserId = JWTUtils.parseJWTByUserId(clueQuery.getToken());
        tClue.setEditBy(loginUserId);//编辑人
        return tClueMapper.updateByPrimaryKeySelective(tClue);
    }

    @Override
    public int delClueById(Integer id) {
        return tClueMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int batchDelClueByIds(List<String> idList) {
        return tClueMapper.deleteByIds(idList);
    }
}
